package com.api.user.controller;
import com.api.base.model.User;
import com.api.base.service.UserService;
import com.api.core.config.AuthUser;
import com.api.core.controller.Ctrl;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.api.user.model.Advertisement;
import com.api.user.model.UserInfo;
import com.api.user.service.AdvertisementService;
import com.api.user.service.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.api.core.annotation.PowerEnable;
import io.swagger.annotations.*;

import static com.api.user.enums.IdentificationStatusEnums.*;


/**
* Created by wanghuiwen on 2021/04/14.
*/
@PowerEnable(name = "广告",url = "/advertisement")
@Api(value = "广告", tags = {"广告"})
@RestController
@RequestMapping("/advertisement")
public class AdvertisementController extends Ctrl{
    @Resource
    private AdvertisementService advertisementService;
    @Resource
    private UserService userService;

    @ApiOperation(value = "广告添加", tags = {"广告"}, notes = "广告添加")
    @PostMapping(value="/add",name="广告添加")
    public Result add(@ApiParam Advertisement advertisement) {
        advertisementService.save(advertisement);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "广告删除", tags = {"广告"}, notes = "广告删除")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",required=true, value = "广告id", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/delete",name="广告删除")
    public Result delete(@RequestParam Long id) {
        advertisementService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "广告修改", tags = {"广告"}, notes = "广告修改,对象主键必填")
    @PostMapping(value="/update",name="广告修改")
    public Result update(@ApiParam Advertisement advertisement) {
        advertisementService.update(advertisement);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "广告详细信息", tags = {"广告"}, notes = "广告详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",required=true, value = "广告id", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/detail",name="广告详细信息")
    public Result detail(@RequestParam Integer id) {
        Advertisement advertisement = advertisementService.findById(id);
        return ResultGenerator.genSuccessResult(advertisement);
    }

    @ApiOperation(value = "广告列表信息", tags = {"广告"}, notes = "广告列表信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "search", value = "查询条件json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/list", name = "广告列表信息")
    public Result list(@RequestParam(defaultValue = "{}") String search,
                       @RequestParam(defaultValue = "{}") String order,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        return advertisementService.list(search, order, page, size);
    }


    @ApiOperation(value = "广告列表信息", tags = {"广告"}, notes = "广告列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", value = "查询条件json", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/list/user", name = "广告列表信息")
    public Result listUser(Authentication authentication) {
        AuthUser authUser = getAuthUser(authentication);
        Map<String,Object> params = new HashMap<>();
        User user = userService.findById(authUser.getId());
        if(user.getType() == USER_PLAYER){
            params.put("advertisementRange",1);
        }
        if(user.getType() == USER_ORGANIZER){
            params.put("advertisementRange",2);
        }

        return advertisementService.listUser(params);
    }
}
